package eproject.mm.com.projectsupervision.util;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import eproject.mm.com.projectsupervision.adapter.PassengerAdapter;

public class PhotoLayoutManage extends GridLayoutManager {
    PassengerAdapter adapter;
    // RecyclerView高度随Item自适应
    public PhotoLayoutManage(Context context, int spanCount, PassengerAdapter _adapter) {
        super(context, spanCount);
        adapter = _adapter;
    }

    @Override
    public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state, final int widthSpec, final int heightSpec) {
        try {
            //不能使用   View view = recycler.getViewForPosition(0);
            //measureChild(view, widthSpec, heightSpec);
            // int measuredHeight  view.getMeasuredHeight();  这个高度不准确

            if (adapter != null && adapter.getItemHeight() > 0) {
                int measuredWidth = View.MeasureSpec.getSize(widthSpec);

                int line = adapter.getItemCount() / getSpanCount();
                if (adapter.getItemCount() % getSpanCount() > 0) line++;
                int measuredHeight = adapter.getItemHeight() * line;
                setMeasuredDimension(measuredWidth, measuredHeight);
            } else {
                super.onMeasure(recycler, state, widthSpec, heightSpec);
            }

        } catch (Exception e) {
            super.onMeasure(recycler, state, widthSpec, heightSpec);
        }
    }
}